package 数组;

import java.util.HashSet;

/**
 * @Author Weton Li
 * @Date 2021/4/23 09:23
 */
public class 存在重复元素_k范围 {
    public static void main(String[] args) {
        int[] input = {1,2,3,1,2,3};
//        int[] input = {1,2,3,1};
        int k = 2;
        System.out.println(method(input, k));
    }

    public static boolean method(int[] arr,int fixdLen) {
        HashSet<Integer> hashSet = new HashSet<>();
        for (int i = 0; i < arr.length; i++) { // 遍历到每一个数
            if (hashSet.contains(arr[i])) {
                return true;
            }
            hashSet.add(arr[i]);
            if (hashSet.size() > fixdLen) { // 1,2,3  3>2
                hashSet.remove(arr[i-fixdLen]);
            }
        }
        return false;
    }
}
